/*
 * StaminaResource.cpp
 *
 *  Created on: 2014. 2. 11.
 *      Author: mega
 */

#include "../User.h"
#include "../NUtil/Utils.h"
#include <Database/Static.h>
#include "StaminaResource.h"
#include "Static.h"

namespace ProjectZ { namespace NResource {

StaminaResource::StaminaResource()
{
	pStaminaAD	= new StaminaData;
	pStaminaIOS	= new StaminaData;

	clsNoneData.maxStamina		= -1;
	clsNoneData.recoveryMinute	= -1;
}

StaminaResource::~StaminaResource()
{
	delete pStaminaAD;
	delete pStaminaIOS;
}

StaminaResource::StaminaData::StaminaData()
{

}

StaminaResource::StaminaData::~StaminaData()
{

}

bool
StaminaResource::LoadResource()
{
	if (false == pStaminaAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pStaminaIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

bool
StaminaResource::StaminaData::LoadResource(int company, int salecode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_event_stamina WHERE COMPANY=%d AND SALE_CODE=%d", company, salecode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int company;
		int salecode;

		clsAuto->GetRecord() >> NATIONAL_CODE >> company >> salecode
				>> _data.maxStamina >> _data.recoveryMinute >> _data.recoveryMinuteEvent;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}
	}

	return true;
}

StaminaResource::StaminaData::Data &
StaminaResource::GetData(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pStaminaAD->GetData();

	case NUtil::MarketType::IOS_KAKAO:
		return pStaminaIOS->GetData();

	default:
		return clsNoneData;
	}
}

int
StaminaResource::GetMaxStamina(User * pUser)
{
	StaminaData::Data & clsData = this->GetData(pUser);
	return clsData.maxStamina;
}

int
StaminaResource::GetRecoveryMinute(User * pUser)
{
	StaminaData::Data & clsData = this->GetData(pUser);

	// 스태미너 복구 시간 감소 이벤트인 경우
	if (true == Static::InstancePtr()->GetEventTime()->IsStaminaRecoveryTimeEvent(pUser))
		return clsData.recoveryMinuteEvent;

	return clsData.recoveryMinute;
}

} /*NResource*/ } /*ProjectZ*/
